package com.anpy.bill.service.impl;

import com.anpy.bill.entity.*;
import com.anpy.bill.mapper.*;
import com.anpy.bill.model.param.MemoDetailParam;
import com.anpy.bill.service.MemoService;
import com.anpy.common.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author : anpy
 * @create 2024/1/22 17:08
 */
@Service
public class MemoServiceImpl extends BaseServiceImpl<MemoMapper, MemoEntity> implements MemoService {

    @Resource
    private MemoDetailMapper memoDetailMapper;
    @Resource
    private MemoDetailImageMapper memoDetailImageMapper;

    @Override
    public List<MemoEntity> memoList(Long userId) {
        return lambdaQuery().eq(MemoEntity::getUserId, userId).list();
    }

    @Override
    public List<MemoDetailEntity> memoDetailList(Long userId, Long memoId) {
        return memoDetailMapper.selectList(
                new LambdaQueryWrapper<MemoDetailEntity>()
                        .eq(MemoDetailEntity::getUserId, userId)
                        .eq(MemoDetailEntity::getMemoId, memoId));
    }

    @Override
    public List<MemoDetailImgEntity> memoDetailImgList(Long userId, Long memoId) {
        return memoDetailImageMapper.selectList(
                new LambdaQueryWrapper<MemoDetailImgEntity>()
                        .eq(MemoDetailImgEntity::getUserId, userId)
                        .eq(MemoDetailImgEntity::getMemoId, memoId));
    }

    @Override
    public boolean saveMemoDetail(MemoDetailParam memoDetailParam) {
        MemoDetailEntity memoDetailEntity = new MemoDetailEntity();
        memoDetailEntity.setTitle(memoDetailParam.getName());
        memoDetailEntity.setShared(memoDetailParam.getShared());
        memoDetailEntity.setContent(memoDetailParam.getCard());
        memoDetailEntity.setUserId(memoDetailParam.getUserId());
        memoDetailEntity.setMemoId(memoDetailParam.getMemoId());
        memoDetailEntity.setCreateTime(new Date());
        memoDetailEntity.setUpdateTime(new Date());
        AtomicInteger imgNum = new AtomicInteger();
        if (memoDetailParam.getImgList() != null) {
            Arrays.stream(memoDetailParam.getImgList().split(",")).forEach(item -> {
                imgNum.getAndIncrement();
                MemoDetailImgEntity photoEntity = new MemoDetailImgEntity();
                photoEntity.setPhotoUrl("http://qnhome.anpy.top/" + item);
                photoEntity.setUserId(memoDetailParam.getUserId());
                photoEntity.setMemoId(memoDetailEntity.getId());
                memoDetailImageMapper.insert(photoEntity);
            });
        }
        memoDetailEntity.setImgNum(imgNum.get());
        memoDetailMapper.insert(memoDetailEntity);
        return true;
    }

    @Override
    public Boolean memoDetailDelete(Long userId, Long memoId) {
        return memoDetailMapper.delete(new LambdaQueryWrapper<MemoDetailEntity>()
                .eq(MemoDetailEntity::getUserId, userId)
                .eq(MemoDetailEntity::getId, memoId)) > 0;
    }
}
